<!DOCTYPE html>
<script src="../../../resources/gesture-util.js"></script>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>

<span id="parent">
    <div id="target" style="border:solid 1px green; width:220px; height:70px; overflow:scroll; white-space:nowrap;">
        TOP<br/><br/><br/><br/>
        Scroll mouse wheel over here<br/><br/><br/><br/>
        END
    </div>
    <div id="target2" style="border:solid 1px blue;">
       And scroll the document here
    </div>
    <div style="height: 2000px;"></div>
</span>

<script>
var scrollAmount = 1;
var last_event = null;
// TODO(crbug.com/1204176): expectedDeltaY will not match expectations if
// percent-based scrolling is enabled.
var expectedDeltaY = scrollAmount * pixelsPerTick();
var testDiv = document.getElementById('target');
var scroller_target = elementCenter(testDiv);

// Reset the page state and last wheel event.
function reset() {
  last_event = null;
  testDiv.scrollTop = 0;
  window.scrollTo(0, 0);
}

function wheelHandler(e) {
  assert_equals(last_event, null);
  last_event = e;
}

document.addEventListener('wheel', wheelHandler);

promise_test(async () => {
  reset();
  await waitForCompositorCommit();
  const wheelPromise = waitForEvent(document, 'wheel');
  await wheelTick(0, scrollAmount, scroller_target, SPEED_INSTANT,
                  Modifiers.CONTROL);
  await wheelPromise;
  await conditionHolds(() => {
    return testDiv.scrollTop == 0;
  });
  assert_equals(last_event.deltaY, expectedDeltaY);
  assert_true(last_event.ctrlKey);
}, "Div: Ctrl+wheel doesn't scroll.");

promise_test(async () => {
  reset();
  await waitForCompositorCommit();
  const wheelPromise = waitForEvent(document, 'wheel');
  await wheelTick(0, scrollAmount, scroller_target, SPEED_INSTANT);
  await wheelPromise;
  await waitForAnimationEndTimeBased(() => { return testDiv.scrollTop; });
  assert_equals(testDiv.scrollTop, expectedDeltaY);
  assert_equals(last_event.deltaY, expectedDeltaY);
  assert_false(last_event.ctrlKey);
}, "Div: No-Ctrl wheel scrolls.");

// Test mousewheel events over the document
var document_element = document.getElementById('target2');
var document_target = elementCenter(document_element);
promise_test(async () => {
  reset();
  await waitForCompositorCommit();
  const wheelPromise = waitForEvent(document, 'wheel');
  await wheelTick(0, scrollAmount, document_target, SPEED_INSTANT,
                  Modifiers.CONTROL);
  await wheelPromise;
  await conditionHolds(() => {
    return document_element.scrollTop == 0;
  });
  assert_equals(last_event.deltaY, expectedDeltaY);
  assert_true(last_event.ctrlKey);
  assert_equals(window.scrollY, 0);
}, "Doc: Ctrl+wheel doesn't scroll.");

// The smoothScrollWithXY here indicates that we set |precise_scrolling_deltas| to
// true to simulate a high precision touch pad which should scroll even if
// control is down, rather than zooming.
promise_test(async () => {
  reset();
  await waitForCompositorCommit();
  const wheelPromise = waitForEvent(document, 'wheel');
  var source = GestureSourceType.MOUSE_INPUT;
  await smoothScrollWithXY(0, scrollAmount* pixelsPerTick(), document_target.x,
                           document_target.y, source, SPEED_INSTANT,
                           true /* precise_scrolling_deltas */,
                           false /* scroll_by_page */,
                           true /* cursor_visible */,
                           false /* scroll_by_percentage */,
                           Modifiers.CONTROL);
  await wheelPromise;
  await waitForAnimationEndTimeBased(() => { return window.scrollY; });
  assert_equals(window.scrollY, expectedDeltaY);
  assert_equals(last_event.deltaY, expectedDeltaY);
  assert_true(last_event.ctrlKey);
}, "Doc: Ctrl+high precision touchpad scroll scrolls.");

promise_test(async () => {
  reset();
  await waitForCompositorCommit();
  const wheelPromise =  waitForEvent(document, 'wheel');
  await wheelTick(0, scrollAmount, document_target, SPEED_INSTANT);
  await wheelPromise;
  await waitForAnimationEndTimeBased(() => { return window.scrollY; });
  assert_equals(window.scrollY, expectedDeltaY);
  assert_equals(last_event.deltaY, expectedDeltaY);
  assert_false(last_event.ctrlKey);
}, "Doc: No-Ctrl wheel scrolls.");

</script>
